home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13405 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: dinews.epfl.ch!usenet
  2. From: Bernhard Ruch <Bernhard.Ruch@studi.epfl.ch>
  3. Newsgroups: comp.lang.c++,gnu.g++.help
  4. Subject: Re: Template function only for built-in types
  5. Date: Mon, 25 Mar 1996 17:25:18 +0100
  6. Organization: Ecole Polytechnique Federale de Lausanne
  7. Message-ID: <3156C8EE.41C6@studi.epfl.ch>
  8. References: <3151BA38.41C6@studi.epfl.ch> <4isjii$fvb@news.aimnet.com>
  9. NNTP-Posting-Host: didec8.epfl.ch
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; OSF1 V3.2 alpha)
  14.  
  15. Ramnivas Laddad wrote:
  16. > No. Problem is not "no operator in istream for class A". And you don't
  17. > need to modify istream for each such class.
  18. > What you need is a function,
  19. > istream& operator >> (istream& rInStream, A& a)
  20. > {
  21. >         // rInStream >> to A's members etc.
  22. >         // ...
  23. >         return rInStream;
  24. > }
  25. > You need to have one such function for each of your class as how to
  26. > associate stream input to class attribute is a class specific matter.
  27. > BTW, looks like you have typedefined istream to InSream. Am I right?
  28.  
  29. No. Perhaps I have to clarify exactly, what my problem is:
  30.  
  31. I want to write two new classes, InStream and OutStream. These two classes
  32. should contain the operators >> (and <<, resp.) for the built-in types, i.e.
  33. I should be able to write:
  34.  
  35.   InStream InStr;
  36.   OutStream OutStr;
  37.   char ch;
  38.   int integ;
  39.   float flo;
  40.   ...
  41.   
  42.   InStr >> ch;
  43.   OutStr << ch;
  44.   InStr >> integ;
  45.   OutStr << integ;
  46.   InStr >> flo;
  47.   OutStr << flo;
  48.   ...
  49.  
  50. But I don't want to be able to write all other types than the built-in types,
  51. especially I don't want to write:
  52.  
  53.   InStr >> A;
  54.  
  55. The only other class that I want to be able to read or write is the class Object,
  56. but that's another problem.
  57.  
  58. The two classes, InStream and OutStream, contain an object of type ifstream, (ofstream resp.).
  59. Now, in order to define the two operators >> and << for all built-in types, I wrote
  60. the two template functions:
  61.  
  62.   template <class T> inline OutStream &operator << (OutStream &OutStr, const T t);
  63.   template <class T> inline InStream  &operator >> (InStream &InStr, T t);
  64.  
  65. Inside these template functions, I use the operators >> and << of the classes ifstream and
  66. ofstream, which should be defined for the built-in types.
  67.  
  68. Unfortunately, the compiler tries to define those two operators not only for the built-in
  69. types, but also for my other classes, A and B, for which the corresponding operators of the
  70. classes istream and ostream do not exist. Therefore, I get the error:
  71.  
  72.   "No match for 'operator >> (class ifstream, class A)'"
  73.  
  74. What I am looking for now is a way to define the operators >> and << for the build-in types
  75. only, and not for all other classes as well.
  76.  
  77. Bernhard
  78.